package com.pig4cloud.pig.auth.endpoint;

import com.pig4cloud.pig.common.core.constant.CacheConstants;
import com.pig4cloud.pig.common.security.annotation.Inner;
import io.springboot.captcha.ArithmeticCaptcha;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 验证码相关的接口
 *
 * @author lengleng
 * @date 2022/6/27
 */
@Inner(false)
@RestController
@RequestMapping("/code")
@RequiredArgsConstructor
public class ImageCodeEndpoint {

	private static final Integer DEFAULT_IMAGE_WIDTH = 100;

	private static final Integer DEFAULT_IMAGE_HEIGHT = 40;
	private final CacheManager cacheManager;

	/**
	 * 创建图形验证码
	 */
	@GetMapping("/image")
	public void image(String randomStr, HttpServletResponse response) throws IOException {
		ArithmeticCaptcha captcha = new ArithmeticCaptcha(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT);
		String result = captcha.text();
		cacheManager.getCache(CacheConstants.DEFAULT_CODE_KEY + randomStr).put(randomStr, result);
		captcha.out(response.getOutputStream());
	}


}
